package com.project.personnel.controller;
import com.project.personnel.entity.*;
import com.project.personnel.service.*;
import com.project.personnel.tools.PageUtilzts;
import com.project.personnel.utils.PageUtil;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
import com.project.personnel.entity.Employee;
import com.project.personnel.service.EmployeeService;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author diaominyh
 * @since 2022-12-12
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    EmployeetrainService employeetrainService;
    @Autowired
    EmployeeremoveService employeeremoveService;
    @Autowired
    AdjustsalaryService adjustsalaryService;
    @Autowired
    EmployeeecService employeeecService;
    @Autowired
    EmployeeretireService employeeretireService;
    @Autowired
    EmployeeService employeeService;

    @RequestMapping("/pageEmployee")
    private PageUtil<Personnelrecord> pageEmployee(Integer page, Integer pageSize) {
        //从每个人事事项表中查询人事事项记录
        List<Employeetrain> employeetrains = employeetrainService.list();
        List<Employeeremove> employeeremoves = employeeremoveService.list();
        List<Adjustsalary> adjustsalaries = adjustsalaryService.list();
        List<Employeeec> employeeecs = employeeecService.list();
        List<Employeeretire> employeeretires = employeeretireService.list();
        //创建人事事项集合
        List<Personnelrecord> personnelrecordList = new ArrayList<>();
        //将查询到的人事记录存放在集合中
        for (Employeeec employeeec : employeeecs) {
            //通过人事事项表中的员工id去员工表中查询员工姓名
            Employee employee = employeeService.getById(employeeec.getEid());
            Personnelrecord personnelrecord = new Personnelrecord(employeeec.getEid(), employee.getName(), "奖惩", employeeec.getEcDate(), employeeec.getRemark());
            personnelrecordList.add(personnelrecord);
        }
        for (Employeetrain employeetrain : employeetrains) {
            Employee byId = employeeService.getById(employeetrain.getEid());
            Personnelrecord personnelrecord = new Personnelrecord(employeetrain.getEid(), byId.getName(), "培训", employeetrain.getTrainDate(), employeetrain.getRemark());
            personnelrecordList.add(personnelrecord);
        }
        for (Employeeremove employeeremove : employeeremoves) {
            Employee employee = employeeService.getById(employeeremove.getEid());
            Personnelrecord personnelrecord = new Personnelrecord(employeeremove.getEid(), employee.getName(), "调动", employeeremove.getRemoveDate(), employeeremove.getRemark());
            personnelrecordList.add(personnelrecord);
        }
        for (Adjustsalary adjustsalary : adjustsalaries) {
            Employee byId = employeeService.getById(adjustsalary.getEid());
            Personnelrecord personnelrecord = new Personnelrecord(adjustsalary.getEid(), byId.getName(), "培训", adjustsalary.getAsDate(), adjustsalary.getRemark());
            personnelrecordList.add(personnelrecord);
        }
        for (Employeeretire employeeretire : employeeretires) {
            Employee employee = employeeService.getById(employeeretire.getEid());
            if (employee != null) {
                Personnelrecord personnelrecord = new Personnelrecord(employeeretire.getEid(), employee.getName(), "调动", employeeretire.getRetireDate(), employeeretire.getRetireReason());
                personnelrecordList.add(personnelrecord);
            }

        }
        //将人事事项按照时间先后的顺序进行排列
        Collections.sort(personnelrecordList, new Comparator<Personnelrecord>() {
            @Override
            public int compare(Personnelrecord o1, Personnelrecord o2) {
                try {
                    if (o1.getTime() == null || o2.getTime() == null) {
                        return -1;
                    }
                    Date date1 = o1.getTime();
                    Date date2 = o2.getTime();
                    if (date1.before(date2)) {
                        return -1;
                    } else if (date1.after(date2)) {
                        return 1;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return 0;
            }
        });
        PageUtil<Personnelrecord> personnelrecordPage = new PageUtil<>();
        personnelrecordPage.page(page, pageSize, personnelrecordList);
        return personnelrecordPage;
    }

    @GetMapping("/employeeAll/{pageIndex}")
    //分页查寻
    public List<Employee> getAll (@PathVariable Integer pageIndex){
        PageUtilzts pageUtil = new PageUtilzts();
        int totalCount = employeeService.totalCount();
        pageUtil.setTotalCount(totalCount);
        pageIndex = (pageIndex - 1) * pageUtil.getPageSize();
        List<Employee> employeeList = employeeService.getAll(pageIndex, pageUtil.getPageSize());
        return employeeList;
    }


    /**
     * 人事记录统计批量删除
     */
    @DeleteMapping("/del")
    public boolean del(@RequestParam("id") List<Integer> list) {
        return employeeService.removeByIds(list);
    }

    /**
     * 导出数据
     */
    @RequestMapping("/export")
    public void exportAll(HttpServletResponse response, @RequestParam(value = "id",required = false) List<Integer> id) {
        List<Employeetrain> employeetrains = employeetrainService.list();
        List<Employeeremove> employeeremoves = employeeremoveService.list();
        List<Adjustsalary> adjustsalaries = adjustsalaryService.list();
        List<Employeeec> employeeecs = employeeecService.list();
        List<Employeeretire> employeeretires = employeeretireService.list();
        //创建人事事项集合
        List<Personnelrecord> personnelrecordList = new ArrayList<>();
        //将查询到的人事记录存放在集合中
        for (Employeeec employeeec : employeeecs) {
            //通过人事事项表中的员工id去员工表中查询员工姓名
            Employee employee = employeeService.getById(employeeec.getEid());
            Personnelrecord personnelrecord = new Personnelrecord(employeeec.getEid(), employee.getName(), "奖惩",employeeec.getEcDate(),employeeec.getRemark());
            personnelrecordList.add(personnelrecord);
        }
        for (Employeetrain employeetrain : employeetrains) {
            Employee byId = employeeService.getById(employeetrain.getEid());
            Personnelrecord personnelrecord = new Personnelrecord(employeetrain.getEid(), byId.getName(), "培训", employeetrain.getTrainDate(), employeetrain.getRemark());
            personnelrecordList.add(personnelrecord);
        }
        for (Employeeremove employeeremove : employeeremoves) {
            Employee employee = employeeService.getById(employeeremove.getEid());
            Personnelrecord personnelrecord = new Personnelrecord(employeeremove.getEid(), employee.getName(), "调动", employeeremove.getRemoveDate(), employeeremove.getRemark());
            personnelrecordList.add(personnelrecord);
        }
        for (Adjustsalary adjustsalary : adjustsalaries) {
            Employee byId = employeeService.getById(adjustsalary.getEid());
            Personnelrecord personnelrecord = new Personnelrecord(adjustsalary.getEid(), byId.getName(), "培训", adjustsalary.getAsDate(), adjustsalary.getRemark());
            personnelrecordList.add(personnelrecord);
        }
        for (Employeeretire employeeretire : employeeretires) {
            Employee employee = employeeService.getById(employeeretire.getEid());
            if (employee != null) {
                Personnelrecord personnelrecord = new Personnelrecord(employeeretire.getEid(), employee.getName(), "调动", employeeretire.getRetireDate(), employeeretire.getRetireReason());
                personnelrecordList.add(personnelrecord);
            }

        }
        //将人事事项按照时间先后的顺序进行排列
        Collections.sort(personnelrecordList, new Comparator<Personnelrecord>() {
            @Override
            public int compare(Personnelrecord o1, Personnelrecord o2) {
                try {
                    if (o1.getTime() == null || o2.getTime() == null) {
                        return -1;
                    }
                    Date date1 = o1.getTime();
                    Date date2 = o2.getTime();
                    if (date1.before(date2)) {
                        return -1;
                    } else if (date1.after(date2)) {
                        return 1;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return 0;
            }
        });
        List<Personnelrecord> personnelrecordList1 = new ArrayList<>();
        if (id!=null){
            for (Integer integer : id) {
                for (Personnelrecord personnelrecord : personnelrecordList) {
                    if (integer == personnelrecord.getEid()) {
                        personnelrecordList1.add(personnelrecord);
                    }
                }
            }
            // 对应excel文件
            HSSFWorkbook wb = new HSSFWorkbook();
            // 对应一页
            HSSFSheet sheet = wb.createSheet("人事记录统计");
            // 对应一行
            HSSFRow row = sheet.createRow(0);
            // 对应一列
            HSSFCell cell = row.createCell(0);
            cell.setCellValue("员工工号");
            cell = row.createCell(1);
            cell.setCellValue("员工姓名");
            cell = row.createCell(2);
            cell.setCellValue("调动/奖惩/培训/调薪/离职");
            cell = row.createCell(3);
            cell.setCellValue("时间");
            cell = row.createCell(4);
            cell.setCellValue("备注");
            if (id.get(0) == 0) {
                idEstimate(personnelrecordList, sheet);
            } else {
                idEstimate(personnelrecordList1, sheet);
            }

            // 生成excel文件并下载到客户端
            try {
                // 设置响应头信息，仅.xls前名字能修改
                response.addHeader("Content-Disposition", "attachment;filename=personnelrecord.xls");
                // 创建输出流
                OutputStream out = response.getOutputStream();
                wb.write(out);
                wb.close();
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    // 导出数据,判断是否选中公共方法
    private void idEstimate(List<Personnelrecord> personnelrecordList, HSSFSheet sheet) {
        Personnelrecord personnelrecord;
        HSSFRow hssfRow;
        HSSFCell hssfCell;
        if (personnelrecordList != null && personnelrecordList.size() > 0) {
            for (int i = 0; i < personnelrecordList.size(); i++) {
                personnelrecord = personnelrecordList.get(i);
                hssfRow = sheet.createRow(i + 1);
                hssfCell = hssfRow.createCell(0);
                hssfCell.setCellValue(personnelrecord.getEid());
                hssfCell = hssfRow.createCell(1);
                if (personnelrecord.getName() != null)
                    hssfCell.setCellValue(personnelrecord.getName());
                else
                    hssfCell.setCellValue("");
                hssfCell = hssfRow.createCell(2);
                if (personnelrecord.getItems() != null)
                    hssfCell.setCellValue(personnelrecord.getItems());
                else
                    hssfCell.setCellValue("");
                hssfCell = hssfRow.createCell(3);
                if (personnelrecord.getTime() != null)
                    hssfCell.setCellValue(personnelrecord.getTime());
                else
                    hssfCell.setCellValue("");
                hssfCell = hssfRow.createCell(4);
                if (personnelrecord.getRemark() != null)
                    hssfCell.setCellValue(personnelrecord.getRemark());
                else
                    hssfCell.setCellValue("");
            }
        }
    }
}
